package com.xpec.c4.db.log;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledFuture;

import com.xpec.c4.db.model.log.basic.BasicLogModel;
import com.xpec.c4.game.time.JobExecutor;

/**
 * business log 專用pool (領取物品 / 回收物品, 訂單資訊需要及時處理, 暫時不加入這個pool裡)
 * @author wuweijun
 *
 */
public class LogBusinessModelPool {
	/**
	 * add接口上一次被调用且执行了SaveLogTask的时间 zhongzhengkai 2011 2011-11-25
	 */
	private static long previousCallTime;

	/**
	 * 使用队列来作为缓冲池
	 */
	public static ConcurrentLinkedQueue<BasicLogModel> logPool = new ConcurrentLinkedQueue<BasicLogModel>();
	public static ScheduledFuture logTask = null;
	static{
		JobExecutor.scheduleAtFixedRate(new SaveBusinessLogCheckTask(), 60*1000, 60*1000);
	}
	/**
	 * 从队列头推出一个对象
	 * 
	 * @return Model对象
	 */
	public static BasicLogModel poll() {
		return logPool.poll();
	}

	/**
	 * 往队列尾部压入一个Model对象
	 * 
	 * @param obj
	 *            Model对象
	 */
	public static void add(BasicLogModel obj) {
		
		// TODO 临时屏蔽
		logPool.offer(obj);
		LogBusinessModelPool.checkTask();
	}

	/**
	 * @return the dealSize
	 */
	public static int getPoolSize() {
		return logPool.size();
	}
	
	public static void checkTask(){
		long time = System.currentTimeMillis();
		// LogModelPool类调用add接口时触发SaveLogTask启动的最短时间
		if ((logTask == null || logTask.isDone()) && (logPool.size() >= LogPoolConstants.POOL_SIZE
				|| (time - LogBusinessModelPool.previousCallTime) >= LogPoolConstants.TIME_SPAN)) {
			LogBusinessModelPool.previousCallTime = time;
			 logTask = JobExecutor.schedule(new SaveBusinessLogTask(), 0);
		}
	}
}
